<?php

class GroupUsers {

    private $groupID;
    private $userID;
    private $description;
    private $user;
    private $group;

    public function getUser() {
        return $this->user;
    }

    public function setUser($user) {
        $this->user = $user;
    }

    public function getGroup() {
        return $this->group;
    }

    public function setGroup($group) {
        $this->group = $group;
    }

    public function getGroupID() {
        return $this->groupID;
    }

    public function setGroupID($groupID) {
        $this->groupID = $groupID;
    }

    public function getUserID() {
        return $this->userID;
    }

    public function setUserID($userID) {
        $this->userID = $userID;
    }

    public function getDescription() {
        return $this->description;
    }

    public function setDescription($description) {
        $this->description = $description;
    }

    public function getListByUserID($db = null) {
        if ($db != null) {

            $sql = "SELECT
                    `GU_G_ID`,
                    `GU_U_ID`,
                    `GU_Description`
                  FROM group_users
                  where GU_U_ID = :GU_U_ID;";

            $parameters = array(':GU_U_ID');
            $values = array($this->getUserID());
            $types = array(PDO::PARAM_INT);

            $result = $db->executeQuery($sql, $parameters, $values, $types);

            // nếu có kết quả
            if ($result != null) {

                // mảng danh sách trả về
                $rows = array();

                // lấy mỗi dòng dữ liệu đưa vào mảng danh sách trả về
                while ($row = $result->fetch()) {
                    $groupUser = new GroupUsers();
                    $groupUser->setGroupID($row->GU_G_ID);
                    $groupUser->setUserID($row->GU_U_ID);

                    $group = new Groups();
                    $group->setID($groupUser->getGroupID());
                    $group = $group->getByID($db);
                    $groupUser->setGroup($group);

                    $rows[] = $groupUser;
                }
                return $rows;
            } else {
                return null;
            }
        }
    }

    public function add($db = null) {
        if ($db != null) {
            $sql = "INSERT INTO `group_users`
                                (`GU_G_ID`,
                                 `GU_U_ID`,
                                 `GU_Description`)
                    VALUES (:GU_G_ID,
                            :GU_U_ID,
                            :GU_Description);";

            $parameters = array(
                ':GU_G_ID', ':GU_U_ID',
                ':GU_Description'
            );

            $values = array(
                $this->getGroupID(), $this->getUserID(),
                $this->getDescription()
            );

            $types = array(
                PDO::PARAM_INT, PDO::PARAM_INT,
                PDO::PARAM_STR
            );
            return $db->executeNonQuery($sql, $parameters, $values, $types, true);
        }
    }

    // delete by groupID and userID
    public function deleteByGroupIDAndUserID($db = null) {
        if ($db != null) {
            $sql = "DELETE
                    FROM `group_users`
                    WHERE `GU_G_ID` = :GU_G_ID
                    AND `GU_U_ID` = :GU_U_ID;";

            $parameters = array(
                ':GU_G_ID', ':GU_U_ID'
            );

            $values = array(
                $this->getGroupID(), $this->getUserID()
            );

            $types = array(
                PDO::PARAM_INT, PDO::PARAM_INT
            );
            return $db->executeNonQuery($sql, $parameters, $values, $types, true);
        }
    }

    public function deleteByUserID($db = null) {
        if ($db != null) {
            $sql = "DELETE
                    FROM `group_users`
                    WHERE `GU_U_ID` = :GU_U_ID;";

            $parameters = array(':GU_U_ID');
            $values = array($this->getUserID());
            $types = array(PDO::PARAM_INT);

            return $db->executeNonQuery($sql, $parameters, $values, $types, true);
        }
    }

    public function getByUserID($db = null) {
        if ($db != null) {
            $sql = "SELECT GU_G_ID,GU_U_ID,GU_Description FROM group_users
                    WHERE GU_U_ID = :GU_U_ID;";
            $parameters = array(':GU_U_ID');
            $values = array($this->getUserID());
            $types = array(PDO::PARAM_INT);

            $result = $db->executeQuery($sql, $parameters, $values, $types, true);
            
            if (count($result) > 0) {

                // mảng danh sách trả về
                $rows = array();

                // lấy mỗi dòng dữ liệu đưa vào mảng danh sách trả về
                while ($row = $result->fetch()) {
                    
                    $groupUser = new GroupUsers();
                    $groupUser->setGroupID($row->GU_G_ID);
                    $groupUser->setUserID($row->GU_U_ID);

                    $group = new Groups();
                    $group->setID($groupUser->getGroupID());
                    $group = $group->getByID($db);
                    $groupUser->setGroup($group);

                    $rows[] = $groupUser;
                }
                return $rows;
            } else {
                return null;
            }
        }
    }

}

?>
